/**
 * Created by yudongyang on 2016/3/31 18:20.
 * mail: yudongyang@kehaoinfo.com
 **/

package com.wujie.activies.controller;

import com.wujie.activies.controller.exchangeBean.JSONData;
import com.wujie.activies.model.Activity;
import com.wujie.activies.model.ActivityType;
import com.wujie.activies.service.IActivityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by yudongyang on 2016/3/31.
 */
@RestController
@RequestMapping("/at")
public class ActivityController {
    private static Logger TRACE = LoggerFactory.getLogger(ActivityController.class);
    @Resource
    private IActivityService activityService;

    @RequestMapping(value = "/{id}",method= RequestMethod.GET)
    public String getActivity(@PathVariable String id){
        JSONData jsonData = new JSONData();
        if(id.equals("all")){
            jsonData.setData(activityService.getAll());
        }else{
            ActivityType at = new ActivityType();
            at.setId(Integer.valueOf(id));
            jsonData.setData(activityService.getByPrimaryKey(Integer.valueOf(id)));
        }
        return jsonData.toString();
    }

    @RequestMapping(value = "/{id}",method= RequestMethod.PUT)
    public String putActivity(@RequestBody Activity activity,@PathVariable int id){
        JSONData jsonData = new JSONData();
        activity.setId(id);
        activityService.updateByPrimaryKey(activity);
        return jsonData.toString();
    }

    @RequestMapping(value = "/{id}",method= RequestMethod.DELETE)
    public String delActivity(@PathVariable int id){
        JSONData jsonData = new JSONData();
        Activity activity = new Activity();
        activity.setId(id);
        activityService.deleteByPrimaryKey(id);
        return jsonData.toString();
    }

    @RequestMapping(value = "/add",method= RequestMethod.POST)
    public String postActivity(@RequestBody Activity activity){
        JSONData jsonData = new JSONData();
        int id = activityService.insertSelective(activity);
        return jsonData.toString();
    }

}
